package zabbix

import (
	"encoding/json"
	"errors"
)

func TriggerprototypeCreateData() (string,map[string]interface{}) {
	method := "triggerprototype.create"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "description": "Free disk space is less than 20% on volume {#FSNAME}",
        "expression": "{Zabbix server:vfs.fs.size[{#FSNAME},pfree].last()}<20",
        "tags": [
            {
                "tag": "volume",
                "value": "{#FSNAME}"
            },
            {
                "tag": "type",
                "value": "{#FSTYPE}"
            }
        ]
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func TriggerprototypeDeleteData() (string,map[string]interface{}) {
	method := "triggerprototype.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
        "12002",
        "12003"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func TriggerprototypeUpdateData() (string,map[string]interface{}) {
	method := "triggerprototype.update"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "triggerid": "13938",
        "status": 0
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func TriggerprototypeGetData() (string,map[string]interface{}) {
	method := "triggerprototype.get"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "output": "extend",
        "selectFunctions": "extend",
        "discoveryids": "22450"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func TriggerprototypeCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := TriggerprototypeCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func TriggerprototypeDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := TriggerprototypeDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func TriggerprototypeUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := TriggerprototypeUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func TriggerprototypeGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := TriggerprototypeGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}



